package com.engine.api.gdyjs.web;

import cn.hutool.core.util.StrUtil;
import com.engine.api.gdyjs.service.AssetsService;
import com.engine.api.gdyjs.service.impl.AssetsServiceImpl;
import com.wbi.util.ParamUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.hrm.company.DepartmentComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AssetsAction {
    AssetsService assetsService = new AssetsServiceImpl();
    private static final Log logger = LogFactory.getLog(AssetsAction.class);
    @GET
    @Path("/getAllAssetsInfo")
    public String getAssets(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception {
        Map<String,Object> stringObjectMap = ParamUtil.request2Map(request);
        Object loginIdObj = stringObjectMap.get("loginId")==null?"":stringObjectMap.get("loginId");
        String loginId = loginIdObj.toString().trim();
        if("portal".equals(loginId)){ // 系统管理员
            return  assetsService.getAssets("0",0,0);
        }
        String sql = "select * from hrmresource where loginid = '" + loginId + "'";
        logger.info("zx-查询登录人："+sql);
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        int qx = 0;
        int userDepartment = 0;
        int id = 0;
        if (rs.next()) {
            userDepartment = StringUtil.isNull(rs.getString("departmentid")) ? 0 : rs.getInt("departmentid");
            id = StringUtil.isNull(rs.getString("id")) ? 0 : rs.getInt("id");
        }
        // 查询权限
        RecordSet qxRs = new RecordSet();
        sql = "select * from cus_fielddata where scopeid = -1 and id = " + id;
        logger.info("zx-查询权限："+sql);
        qxRs.executeQuery(sql);
        if (qxRs.next()) {
            qx = StringUtil.isNull(qxRs.getString("field0")) ? 0 : qxRs.getInt("field0");
        }
        logger.info("zx-qx: " + qx);
        logger.info("zx-userDepartment: " + userDepartment);
        List<String> childList = new ArrayList<>();
        childList.add(DepartmentComInfo.getAllChildDepartId(String.valueOf(userDepartment), String.valueOf(userDepartment)));
        String join = String.join(StrUtil.COMMA, childList);
        logger.info("join: " + join);
        return  assetsService.getAssets(join,qx,userDepartment);
    }
}
